<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>
<body>
    <h3>请输入要猜的数字:</h3>
    <input type="text">
    <div>
        已经猜的次数: <span id="guessCount">0</span>
    </div>
    <div>
        结果: <span id="result"></span>
    </div>
    <button>猜</button>

    <script>
        //0.获取到需要的元素
        let input=document.querySelector('input');
        let button=document.querySelector('button');
        let guessCount=document.querySelector('#guessCount');
        let result=document.querySelector('#result');

        //1.在页面加载的时候,要生成1-100之间随机的整数
        let toGuess=parseInt(Math.random()*100)+1;
        console.log(toGuess)

        //2.给猜这个按钮加个点击事件
        button.onclick=function(){
            //获取到输入框里的值
            let value=parseInt(input.value);
            //和当前生成的数字进行对比
            if(toGuess>value){
                result.innerHTML='低了';
            }else if(toGuess==value){
                result.innerHTML='猜对了';
            }else{
                result.innerHTML='高了';
            }
            //没点一次提交,猜的次数+1
            let guessCountValue=parseInt(guessCount.innerHTML);
            guessCount.innerHTML=guessCountValue+1;
        }
    </script>
</body>
</html>